
package com.regal.packet;

import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.provider.PacketExtensionProvider;
import org.xmlpull.v1.XmlPullParser;

public class NewsPushProvider
    implements PacketExtensionProvider
{

    public NewsPushProvider()
    {
    }

    public PacketExtension parseExtension(XmlPullParser parser)
        throws Exception
    {
    	NewsPush newsPushPacket = new NewsPush();
        boolean done = false;
        NewsPush.NewsItem newsItem = null;
        
        System.out.println("getName is "+parser.getName());
                
        while (!done) {
            int eventType = parser.next();
            //System.out.println("eventType is "+eventType);
            //System.out.println("name is "+parser.getName());
            if (eventType == XmlPullParser.START_TAG) {
                if (parser.getName().equals("item")) {
                    String title = parser.getAttributeValue("", "title");
                    String url = parser.getAttributeValue("", "url");
                    // Create packet.
                    System.out.println("getItem title:"+title+"url"+url);
                    newsItem = new NewsPush.NewsItem(title, url);
                    newsPushPacket.addNewsItem(newsItem);
                	System.out.println("addItem title:"+newsItem.getTitle()+"url"+newsItem.getUrl());
                }
            }
            else if (eventType == XmlPullParser.END_TAG) {
                if (parser.getName().equals("item")) {
                	System.out.println("end of a item");
                }
                if (parser.getName().equals("query")) {
                    done = true;
                }
            }
            
        }
        return newsPushPacket;
    	
    }
}    
